package cn.edu.xust.service.impl;

import cn.edu.xust.dao.SysUserroleDao;
import cn.edu.xust.entity.SysUserroleEntity;
import cn.edu.xust.service.SysUserroleService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysUserroleService")
public class SysUserroleServiceImpl extends ServiceImpl<SysUserroleDao, SysUserroleEntity> implements SysUserroleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserroleEntity> page = this.page(
                new Query<SysUserroleEntity>().getPage(params),
                new QueryWrapper<SysUserroleEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<SysUserroleEntity> getByUid(Integer uid) {
        QueryWrapper<SysUserroleEntity> wap = new QueryWrapper<SysUserroleEntity>().eq("uid", uid);
        return baseMapper.selectList(wap);
    }

    @Override
    public List<SysUserroleEntity> getByroleid(Integer roleid) {
        QueryWrapper<SysUserroleEntity> wap = new QueryWrapper<SysUserroleEntity>().eq("roleid", roleid);
        return baseMapper.selectList(wap);

    }

    @Override
    public boolean resetByuid(Integer uid, List<Integer> checked_role) {

        //删除
         this.removeByUid(uid);

        //添加
        List<SysUserroleEntity> sysUserhotelEntities = new ArrayList<>();
        for (Integer integer : checked_role) {
            SysUserroleEntity sysUserhotelEntity = new SysUserroleEntity();
            sysUserhotelEntity.setUid(uid);
            sysUserhotelEntity.setRoleid(integer);
            sysUserhotelEntities.add(sysUserhotelEntity);
        }
        return this.saveBatch(sysUserhotelEntities);

    }

    @Override
    public boolean removeByUid(Integer uid) {
        QueryWrapper<SysUserroleEntity> wap = new QueryWrapper<SysUserroleEntity>().eq("uid", uid);
        return baseMapper.delete(wap) > 0;

    }

}
